import $ from "jquery";

class CreateWineType {
    constructor(selector){
        this.init.apply(this,arguments);
    }
    init(selector){
        this.m_ulEle = $(selector);
        this.getAjaxData();
    }
    getAjaxData(){
        $.ajax({
            url: "./server/parts/inc/home.json",
            type: "GET",
            dataType: "json",
            success: $.proxy(this.render,this)
        });
    }
    render(res){
        let data = res.data;
        if(!data) return;
        let html = "";
        let liName = ["lf_allA","lf_allB","lf_allC","lf_allD"];
        data.forEach((info,index)=>{
            html += `<li class="left_onname ${liName[index]} ${index == (data.length - 1) && "noborder"}">
                <div class="fl_biaoti">${info.title}</div>
                ${this.createTabUI(info.list)}
                <div class="clear"></div>
                ${this.createDetails(info.details)}
            </li>`;
        })
        this.m_ulEle.html(html);
    }
    createTabUI(list){
        let html = `<div class="xiao_lei">`
        $.each(list,(index,item)=>{
            let className = item.hasColor ? 'class="hover"' : "";
            html += `<a ${className} href= "${item.lk}" >${item.name}</a>`
        })
        html += `</div>`;
        return html;
    }
    createDetails(list){
        if(!list || list.length <= 0) return "";
        let html = `<div class="on_biaoti">`;
        $.each(list,(index,item)=>{
            let tab = item.tab;
            let kind = item.kind;
            html += `<dl>`;
            
            html += tab.lk ? `<dt>
                <a href = "${tab.lk}">
                    <h2>${tab.title}</h2>
                </a>
            </dt>`: `<dt><h2>${tab.title}</h2></dt>`;

            html += "<dd>";
            $.each(kind,(i,temp)=>{
                html += `<a href= "${temp.lk}">${temp.name}</a>`;
            })
            html += "</dd>";

            html += `<div class="clear"></div>`;
            html += `</dl>`;
        });
        html += `</div>`;
        return html;
    }
}

module.exports = CreateWineType;
